package com.oauth.twitter.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.twitter.client.beans.TwitterModel;
import com.oauth.twitter.client.beans.TwitterOAuthVerifier;
import com.oauth.twitter.client.rpc.TwitterRemoteService;
import com.oauth.twitter.client.rpc.TwitterRemoteServiceAsync;

public class MainPanel extends Composite {

	private static MainPanelUiBinder uiBinder = GWT
			.create(MainPanelUiBinder.class);

	interface MainPanelUiBinder extends UiBinder<Widget, MainPanel> {
	}

	@UiField
	DecoratedStackPanel stackPanel;

	public static final String applicationName = "Twitter";
	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final TwitterRemoteServiceAsync twitterService = GWT
			.create(TwitterRemoteService.class);

	@UiField
	LoginPanel loginPanel;

	@UiField
	HomePanel homePanel;

	@UiField
	DeckPanel deckPanel;

	public MainPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		loginPanel.setService(twitterService);
		homePanel.setService(twitterService);
		stackPanel.setStackText(0, "<div class=\"stackText\">"
				+ applicationName + "</div>", true);

	}

	public void initMainPanel() {
		String oauthVerifier = Window.Location.getParameter("oauth_verifier");
		TwitterOAuthVerifier queryString = new TwitterOAuthVerifier(
				oauthVerifier);
		deckPanel.showWidget(0);
		twitterService.getTweets(queryString,
				new OAuthAsnyCallBack<TwitterModel>() {

					public void onSuccess(TwitterModel result) {
						if (result.isLoginView()) {
							deckPanel.showWidget(1);
						} else {
							homePanel.setData(result);
							deckPanel.showWidget(2);
						}
					}
				});
	}
}